package com.ifreeee.practice;

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

/**
 *
 update ELECTRICITY_SUPPLIER set parent_org_no=substr(org_no, 1, 5), TYPE = '供电所'
 where length(org_no)=7;

 update ELECTRICITY_SUPPLIER set parent_org_no='11102', TYPE = '供电公司'
 where length(org_no)=5;

 update ELECTRICITY_SUPPLIER set parent_org_no='0', TYPE = '供电公司'
 where org_no=11102;

 select * from ELECTRICITY_SUPPLIER order by ID;
 */
public class OrgNoName {
    public static void main(String[] args) {
        try {

            // 创建一个新的文档构建器工厂实例
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            
            // 使用文档构建器工厂创建新的文档构建器实例
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

            // 解析XML文件并获取文档对象
            Document doc = dBuilder.parse("D:\\repository\\pro\\spring-view\\spring-view\\src\\main\\resources\\temp.xml");

            // 获取文档元素
            doc.getDocumentElement().normalize();

            // 遍历文档中的所有元素节点，并打印它们的内容
            NodeList nList = doc.getElementsByTagName("Row");
            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    // 两个data
                    NodeList nl = eElement.getElementsByTagName("Data");
                    String dw = "";
                    String no = "";
                    ctn:for (int t = 0; t < nl.getLength(); t++) {
                        Node n = nl.item(t);
                        NamedNodeMap attr = n.getAttributes();
                        for (int i = 0; i < attr.getLength(); i++) {
                            Node node = attr.item(i);
                            if("ss:Type".equals(node.getNodeName())){
                                if("String".equals(node.getNodeValue())){
                                    dw = n.getTextContent();
                                    continue ctn;
                                }
                                if("Number".equals(node.getNodeValue())){
                                    no = n.getTextContent();
                                    continue ctn;
                                }
                            }
                        }
                    }
                    System.out.println(no + ", " + dw);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}